package com.fingard.dsp.bank.directbank.abc02;

import com.fingard.dsp.bank.directbank.bankInterface.IDeContract;
import com.fingard.dsp.bank.reqrespobj.contract.ContractState;
import com.fingard.dsp.bank.reqrespobj.decontract.ReqDeContract;
import com.fingard.dsp.bank.reqrespobj.decontract.RetAtsDeContract;
import com.fingard.dsp.bank.reqrespobj.decontract.detail.RetAtsDeContractDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.util.Date;

/**
 * 中国农业银行银企直联公网版(ABC02)解约
 */
public class ABC02DeContract extends ABC02Base implements IDeContract {
    @Override
    public RetAtsDeContract deContract(ReqDeContract p_req) throws Exception {
        RetAtsDeContract retAts = new RetAtsDeContract(p_req.reqHeader);
        RetAtsDeContractDetail tmpRetDetail = new RetAtsDeContractDetail(p_req);

        try {
            XmlTextWriter xmlWriter = new XmlTextWriter();
            ABC02Header bankHeader = new ABC02Header();

            bankHeader.CCTransCode = ABC02TransCode.deal_decontract;
            bankHeader.ReqSeqNo = p_req.contractDetail.askNumber;
            bankHeader.ReqDateTime = new Date();

            writeCommonHeader(xmlWriter, bankHeader);

            xmlWriter.writeStartElement("Cmp");
            xmlWriter.writeElementString("DbProv", getBankConfig().getMapValueByKey("dbProvCFRTC6", p_req.contractDetail.recAct));
            xmlWriter.writeElementString("DbAccNo", getAbcAccNo(p_req.contractDetail.recAct));
            xmlWriter.writeElementString("DbCur", "01");
            xmlWriter.writeElementClosed("DbLogAccNo");
            xmlWriter.writeElementString("CrProv", p_req.contractDetail.payArea);
            xmlWriter.writeElementString("CrAccNo", getAbcAccNo(p_req.contractDetail.payAct));
            xmlWriter.writeElementString("CrCur", "01");
            xmlWriter.writeElementClosed("CrLogAccNo");
            xmlWriter.writeEndElement();

            xmlWriter.writeStartElement("Corp");
            xmlWriter.writeElementClosed("DbAccName");
            xmlWriter.writeElementClosed("CrAccName");
            xmlWriter.writeElementString("NFAccNo", getBankConfig().getMapValueByKey("nFAccNoCFRTC6", p_req.contractDetail.recAct));
            xmlWriter.writeElementString("NVoucherType", getBankConfig().getValueAsString("nVoucherTypeCFRTC6"));
            xmlWriter.writeElementString("ContPhone", p_req.contractDetail.cellPhone);
            xmlWriter.writeEndElement();

            xmlWriter.writeStartElement("Acc");
            xmlWriter.writeElementString("AccType","1");
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();

            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());

            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRespCode = tmpBkReader.getFirstText("/ap/RespCode");
                    String tmpRetInfo = tmpBkReader.getFirstText("/ap/RespInfo");
                    String tmpRetRxtInfo = tmpBkReader.getFirstText("/ap/RxtInfo");
                    String tmpRespSource = tmpBkReader.getFirstText("/ap/RespSource");
                    if (tmpRespSource != null && "0".equals(tmpRespSource) && StringHelper.hasAnyChar(tmpRespCode)) {
                        tmpRetDetail.respCode = "0";
                        tmpRetDetail.respInfo = StringHelper.joinNotEmpty("+", tmpRetInfo, tmpRetRxtInfo);
                        if ("0000".equals(tmpRespCode)) {
                            tmpRetDetail.deSignState = ContractState.SUCCESS;
                        } else {
                            tmpRetDetail.deSignState = ContractState.FAIL;
                        }
                    } else {
                        tmpRetDetail.respCode = "-1";
                        tmpRetDetail.respInfo = tmpRetInfo;
                    }
                } else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = tmpStrRet[1];
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }

    @Override
    public RetAtsDeContract qryDeContract(ReqDeContract p_reqQry) throws Exception {
        return null;
    }
}